package com.lzxuni.modules.dataAcquisition;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lzxuni.common.annotation.SysLog;
import com.lzxuni.common.utils.R;
import com.lzxuni.common.utils.StringUtils;
import com.lzxuni.modules.common.controller.BaseController;
import com.lzxuni.modules.common.entity.PageData;
import com.lzxuni.modules.common.entity.PageParameter;
import com.lzxuni.modules.common.entity.Tree;
import com.lzxuni.modules.dataAcquisition.entity.Chain;
import com.lzxuni.modules.dataAcquisition.service.ChainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 追溯链条 前端控制器
 * </p>
 *
 * @author wzx
 * @since 2019-06-25
 */
@RestController
@RequestMapping("/LR_DataAcquisition/Chain")
public class ChainController extends BaseController {

    @Autowired
    private ChainService chainService;

    @RequestMapping("/Index")
    @SysLog(categoryId = 2, module = "追溯链条", operateType = "访问")
    public ModelAndView list() {
        ModelAndView mv = new ModelAndView("/LR_DataAcquisition/Chain/index");
        return mv;
    }

    @RequestMapping("/GetList")
    public Object GetList(String pagination, Chain chain, String Keyword) throws SQLException {
        PageParameter pageParameter = JSON.parseObject(pagination, PageParameter.class);
        chain.setChainName(Keyword);
        PageData pageData = getPageData(chainService.queryPage(pageParameter, chain));
        return R.ok().put("data", pageData);
    }

    @RequestMapping("/Form")
    public ModelAndView insert(String keyValue) throws Exception {
        ModelAndView mv = new ModelAndView("/LR_DataAcquisition/Chain/form");
        String code = "";
        if (StringUtils.isNotEmpty(keyValue)) {
            Integer i = chainService.count();
            if(i == 0){
                code = "1001";
            }else{
                List<Chain> chains = chainService.list(new QueryWrapper<Chain>().orderByDesc("create_time"));
                int c = Integer.parseInt(chains.get(0).getChainCode());
                c++;
                code = String.valueOf(c);
            }
        }
        mv.addObject("code", code);
        return mv;
    }

    @RequestMapping("/SaveForm")
    @SysLog(categoryId = 3, module = "追溯链条", operateType = "增/改")
    public Object insertDo(HttpServletRequest request, String keyValue, Chain chain) throws Exception {
        if (StringUtils.isEmpty(keyValue)) {
            chain.setCreateTime(new Date());
            chainService.save(chain);
            return R.ok("保存成功");
        } else {
            chain.setChainId(keyValue);
            chainService.updateById(chain);
            return R.ok("修改成功");
        }
    }

    @RequestMapping("/DeleteForm")
    @SysLog(categoryId = 3, module = "追溯链条", operateType = "删除")
    public Object delete(String keyValue) {
        chainService.removeById(keyValue);
        return R.ok("删除成功");
    }

    @RequestMapping("/GetSelectTree")
    public Object GetSelectTree(String nature)  {
        List<Tree> trees = chainService.getTreeSelectTree(nature);
        return R.ok().put("data",trees);
    }

}

